Welcome back. So, we've seen ways to get
a value associated with a particular key in a dictionary.
So, for example, if we have this dictionary and we assign it to
the variable inventory and it has four key value pairs.
If we wanted the value associated with the key oranges,
then we would say something like print, inventory sub oranges.
But sometimes we don't want the value associated with just one key,
we want the value associated with every key in that dictionary or
we want to iterate over every key value pair in that dictionary.
In order to do that, dictionaries have a set of methods that we'll find useful.
So, the first method that we'll find useful is.keys.
So, if we want to loop over every key in our dictionary by using a for loop.
So, if I say, for, key in inventory.keys.
Then, for now I'm just going to print out the key.
So I'll say print key.
When I run this code,
then I'll see that I get all of the keys in my dictionary.
Remember that dictionaries aren't ordered,
so there are no guarantees what order I'll actually get these keys in.
The only guarantee is that this for loop
is going to run once for every key regardless of order.
So, when I just print out the key then I can see that there's apples, bananas,
oranges, and pears are all of the four keys in our dictionary.
If I want to get the value associated with that key.
I can say. "Key has the value".
Then to get the value associated with the key,
I use Inventory sub key.
Now, notice here that I'm not putting key in quotation marks.
In other words, what I want is I want the value of the variable key.
I don't want to look for the value associated with the key, named key.
So, like if this dictionary had
a key named key
associated with the value 50 and I put key in quotation marks then I would get 50.
But here instead, I'm saying I want
key the variable's value to be the key that I'm fetching.
So, now we'll see apples has the value 430,
bananas has the value 312,
oranges has the value 525,
and pears has the value 217.
So, in order to actually get that list of keys into a list.
One thing that I can do is I can just say,
keys equals a list of inventory.keys.
If I now print out the value of keys,
then I should get a list of keys in our dictionary inventory.
But remember again, that there's no guarantee about order in here.
So, these keys could be in any order.
The only guarantee is that I'm going to get
a list that has all of the keys in some order.
Now, notice here that when I got the list of
keys I cast it to a list by calling the list function.
The reason that we'll do that we're not going to get into too much
yet but on a high level we always cast to
a list because in Python Three
Inventory.keys doesn't actually quite return a list directly.
It returns something that we can actually iterate over,
so we can put inventory.keys in our for loop but in order to actually get
a list of keys we're always going to have to cast it
to a list by calling the list function.
If we wanted to iterate over inventory in a slightly less verbose way,
we could just say for k in inventory.
So, whereas here we're saying for key in inventory.keys
here we're just saying for- I'll just rename this key to be consistent,
for every key in inventory.
When we say that then Python automatically assumes that we want to iterate over the keys.
So, when I run this code then I say.key and then every key in
our dictionary again no guarantees about
order just that we'll loop through every single key that we have.
Dictionaries have two other methods that are somewhat similar to.keys.
So,.values rather than getting a list of keys gets a list of values,
so 430, 312, 525, and 217.
So,.values would give us a list that has all of
these integers but again there are no guarantees about ordering.
So, we know that it's going to be a list with these four items,
we just don't know what order inventory.values is going to be in.
Inventory.items instead gives us a list of key value pairs as tuples.
So, inventory.items is going to give us a list.
And the first item might
be apples and 430.
Again, I say might be because we don't know what the ordering is,
but the second item might be oranges associated with the value 525,
and then we might have bananas and so on.
So,.item gives us a list of tuples where every tuple is a key value pair.
So on line three we print out the value of
inventory.values and on line four we print out the value of inventory.
Items. So, let's see what these two lines output.
I'm going to for now comment out lines six and seven.
So, we can see that when we printed it out
inventory.values then we got all of the values.
They aren't necessarily going to be in the order that we actually created
the dictionary in but in this case they just happened to be in that order.
When recalled inventory.items and you can see that we have
a list of tuples of key-value pairs.
So here, this first tuple says that the value of the key apples is 430,
the value of the key bananas is 312, and so on.
We can also use the same inoperator that we saw on lists and strings on dictionaries.
So, if we print out the value of this expression apples in inventory,
then the value of this expression is going to be a Boolean.
The value of that Boolean is going to be true,
if this is a key in our dictionary.
So, if apples is
a key in
inventory and false otherwise.
Now, it's crucial here that I mention that it's true only
if apples is a key in our dictionary.
It can't be a value it has to be a key.
So, when we print out the value of apples in inventory,
then we can see here that we have a key-value pair where the key is apples.
So, this should be true.
If we print out the value of cherries in inventory,
then if we look at our key-value pairs apples, bananas, oranges,
pears, we don't see anything that has the key cherries,
so this is going to be false.
So, if we comment out this code and just run
lines two and three then we should see the value of apples and inventory is true,
the value of cherries and inventory is false.
Now, we can also write code that depends on the values of these Boolean expressions.
So, we can say, if our dictionary has the key bananas, by saying.
If bananas in inventory.
Again, this expression is a Boolean that's true if bananas is a key in our dictionary,
in this case, bananas is a key.
So, if that key is in our dictionary which in this case it is,
then we print out the value of inventory sub bananas.
Otherwise, if bananas is not a key,
we say we have no bananas.
So, here we're going to print out 312 which is the value associated with bananas.
If I modified this key to be something else,
I'll literally call it something else.
Then, we would see we have no bananas,
because bananas is no longer a key in our dictionary.
Another method that we can use on dictionaries is.get.
So,.get works almost just like indexing.
So, we can say inventory.get apples in the value of this expression is going to be
the same as the value of inventory sub apples,
which in this case is going to be 430.
So, if I just run line three and comment out lines four and six here,
then I see that I get 430.
Now, on line four we say print out inventory.get cherries.
Now, notice here that cherries is not in our dictionary,
so we don't have any key whose value is cherry.
So, when we run this code,
when we get the value none.
This is the difference between.get and actually indexing.
Because if we indexed here,
so if we said print out inventory sub cherries,
we would instead get a runtime error because cherries was not a key in our dictionary.
If we instead called.get then we get kind of a softer error.
So, instead of actually giving us a runtime error that stops our program.get
says the value is none..get also takes an optional second argument,
which is to say that if this key isn't
there then this is what the value of that expression should be.
So, here when we say inventory.get
cherries and then we pass in a second argument of zero,
then this is going to say,
if cherries is in our dictionary as a key then get the value associated with it.
If cherries is not a key in our dictionary,
then just use this as the value.
This isn't going to add a key value pair with cherries,
it's instead just going to say the value of this overall expression should be zero.
So, when we run our code,
we see that now when we call inventory.get cherries with the optional argument zero here,
we instead get zero because cherries isn't a key in our dictionary.
If we had 999,
then we would get 999.
If we had a key cherries in our dictionary,
but say it's five,
then we would instead get five.
That's all for now until next time.